<template>
	<view>
		<view>
			<uni-card style="" v-for="item in pendingApproList" :key="item.id" :title="item.applyNo"
				:extra="item.className" @click="toDetail(item)">
				<view style="display: flex; padding: 40rpx 20rpx;">
					<image style="width:200rpx; height: 250rpx;"
						:src="item.studentImg !=''? item.studentImg:'https://tj-1305380059.cos.ap-nanjing.myqcloud.com/pic/boy.png'"
						@click.stop="clickImg(item.studentImg)" mode=""></image>
					<view class="" style="font-size: 24rpx; margin-left: 60rpx; width: 280rpx;">
						<view>学生姓名：{{item.studentName}}</view>
						<view>未归日期：{{item.notRetrunedDate}}</view>
						<view>未归原因：{{item.reason}}</view>
						<view>当前审批人：{{item.currentApprover}}</view>
						<view>当前审批节点：{{item.currentNode}}</view>
						<view style="display: flex;">申请单状态：
							<view v-if="item.applyStatus == 0"><text>申请被驳回</text></view>
							<view v-if="item.applyStatus == 1" style="color: #fe0000;"><text>处理中</text></view>
							<view v-if="item.applyStatus == 2"><text>申请被撤回</text></view>
							<view v-if="item.applyStatus == 10"><text style="color: green;">已处理</text></view>
						</view>
					</view>
				</view>
				<view v-if="item.applyStatus == 1" style="padding: 20rpx 0;">
					<view class="button" @click.stop="cancel(item)">撤 销</view>
				</view>
			</uni-card>
			<!-- 弹框 -->
			<uni-popup ref="popup" background-color="#fff" type="center">
				<view style="width: 40vh; padding: 60rpx 20rpx;">
					<uni-forms ref="form" :rules="rules" :modelValue="formData" label-width="90px">
						<uni-forms-item required label="宿舍号" name="sdId">
							<uni-data-select v-model="formData.sdId" @change="checkDorNo"
								:localdata="dorNoList"></uni-data-select>
						</uni-forms-item>
						<uni-forms-item required label="宿舍学生" name="studentId">
							<uni-data-select @change="checkStudent" v-model="formData.studentId"
								:localdata="studentList"></uni-data-select>
						</uni-forms-item>
						<uni-forms-item required label="未归寝日期" name="notReturnedDate">
							<uni-datetime-picker type="date" :clear-icon="false" :start="Date.now()"
								v-model="formData.notReturnedDate" @maskClick="checkDate" />
						</uni-forms-item>
						<uni-forms-item v-if="studentShow" label="学生照片" name="studentImg">
							<image style="width: 200rpx; height: 200rpx;" :src="formData.studentImg" mode=""></image>
						</uni-forms-item>
						<uni-forms-item v-if="studentShow" label="班主任" name="teacherName">
							<uni-easyinput disabled v-model="formData.teacherName"></uni-easyinput>
						</uni-forms-item>
						<uni-forms-item v-if="studentShow" label="班级名称" name="className">
							<uni-easyinput disabled v-model="formData.className"></uni-easyinput>
						</uni-forms-item>
						<uni-forms-item required label="未归寝原因" name="reason">
							<uni-easyinput type="textarea" v-model="formData.reason"></uni-easyinput>
						</uni-forms-item>

						<view style="display: flex;">
							<button size="default" type="primary" style="width: 40%;" @click="submit">提交</button>
							<button size="default" style="width: 40%;" @click="closePopup">返回</button>
						</view>

						<!-- <button class="button" @click="submit" type="primary" style="width: 125%;">提交</button> -->
					</uni-forms>
				</view>
			</uni-popup>
		</view>
	</view>
</template>

<script>
	import managementTeacher from "@/api/modules/managementTeacher.js"
	export default {
		data() {
			return {
				topName: "未归寝",
				userName: "",
				queryParams: {
					pageSize: "100",
					pageNum: "1",
				},
				// 表单数据
				pendingApproList: [],
				// 表单数据
				formData: {
					sdId: "",
					studentId: "",
					notReturnedDate: "",
					studentImg: "",
					teacherName: "",
					className: "",
					reason: "",
				},
				// 宿舍号列表
				dorNoList: [],
				// 学生列表
				studentList: [],
				// 学生信息显示
				studentShow: false,
				// 班级id
				classId: "",
				// 表单校验
				rules: {
					sdId: {
						rules: [{
							required: true,
							errorMessage: '宿舍号不能为空'
						}]
					},
					studentId: {
						rules: [{
							required: true,
							errorMessage: '学生不能为空'
						}]
					},
					notReturnedDate: {
						rules: [{
							required: true,
							errorMessage: '未归寝日期不能为空'
						}]
					},
					reason: {
						rules: [{
							required: true,
							errorMessage: '未归寝原因不能为空'
						}]
					},
				},
			}
		},
		methods: {
			toDetail(item) {
				console.log(item)
				uni.navigateTo({
					url: `/pages_main/managementTeacher/itemDetailHouse/itemDetailHouse?item=${JSON.stringify(item)}&flag=9`
				})
			},
			// 图片预览
			clickImg(img) {
				uni.previewImage({
					urls: [img], //需要预览的图片http链接列表，多张的时候，url直接写在后面就行了
					current: '', // 当前显示图片的http链接，默认是第一个
					success: function(res) {},
					fail: function(res) {},
					complete: function(res) {},
				})
			},
			// 表单清空
			reset() {
				this.formData = {
					sdId: "",
					studentId: "",
					notReturnedDate: "",
					studentImg: "",
					teacherName: "",
					className: "",
					reason: "",
				}
				this.classId = ""
			},
			// 获取申请单列表
			getPageList() {
				managementTeacher.getPagelist(this.queryParams).then(res => {
					this.pendingApproList = res.data.list
					console.log(this.pendingApproList)
				})
			},
			toggle() {
				this.$refs.popup.open()
			},
			// 获取所有宿舍号
			getAppDormitoryList() {
				managementTeacher.getAppDormitoryList().then(res => {
					res.data.forEach(item => {
						let obj = {}
						obj.text = item.buildingNo + item.dormitoryNo
						obj.value = item.bdormitoryId
						this.dorNoList.push(obj)
					})
				})
			},
			checkDate(e) {
				console.log()
			},
			// 选择宿舍号
			checkDorNo(e) {
				this.formData.studentId = ""
				this.studentShow = false
				if (e) {
					this.getStudentByDormitoryId()
				}
			},
			// 根据宿舍号查学生列表
			getStudentByDormitoryId() {
				managementTeacher.getStudentByDormitoryId(this.formData.sdId).then(res => {
					res.data.forEach(item => {
						let obj = {}
						obj.text = item.studentName
						obj.value = item.id
						this.studentList.push(obj)
					})
				})
			},
			// 拿到学生信息
			checkStudent(e) {
				this.studentShow = true
				managementTeacher.getStudentById(e).then(res => {
					this.formData.studentImg = res.data.stuPhoto
					this.formData.className = res.data.className
					this.classId = res.data.classId
					this.formData.teacherName = res.data.teacherName
				})
			},
			// 提交按钮
			submit() {
				this.$refs.form.validate((err, formData) => {
					if (!err) {
						managementTeacher.createApply({
							applyId: "notRetrunApplyNew",
							applyStatus: "1",
							classId: this.classId,
							notRetrunedDate: this.formData.notReturnedDate,
							reason: this.formData.reason,
							sdId: this.formData.sdId,
							studentId: this.formData.studentId,
							createBy: this.userName
						}).then(res => {
							this.studentShow = false
							this.getPageList()
							this.$refs.popup.close()
							this.reset()
						}).catch(error => {
							console.log(error)
							uni.showToast({
								icon: "none",
								title: error.data.message
							})
							this.reset()
						})
					}
				}).then(res => {

				})
			},
			// 撤销申请
			cancel(item) {
				uni.showModal({
					content: '是否确认撤销申请',
					success: (res) => {
						if (res.confirm) {
							managementTeacher.cancelApply({
								processInstanceId: item.processInstanceId
							}).then(res => {
								this.getPageList()
							})
						} else if (res.cancel) {

						}
					}
				});
			},
			closePopup() {
				this.reset()
				this.studentShow = false
				this.$refs.popup.close()
			}
		},
		onLoad() {
			this.userName = uni.getStorageSync("userName")
			this.getAppDormitoryList()
			this.getPageList()
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.indexContent {
		height: 100vh;
	}

	.button {
		width: 50%;
		background-color: #22ad38;
		color: #fff;
		padding: 20rpx;
		border-radius: 10rpx;
		text-align: center;
		font-size: 36rpx;
		margin: 0 auto;
	}
</style>